-- Connection information.
host = "localhost"
port = 10004

require("remoteCalls");

-- Helper function to get a window by name.
function getWindow(name)
    return CEGUI.WindowManager:getSingleton():getWindow(name);
end

-- Login button clicked.
function loginClicked(eventArgs)
    -- Check the login information.
    local username = getWindow("root/login/name"):getText();
    local password = getWindow("root/login/pass"):getText();
    odysic.sendLoginPacket(username, password);
end

-- Tick function
function tick()
    serverDebugMessage("This is a test!");
end

-- Got a login fail packet.
function gotLoginFailPacket()
end

-- Got a login OK packet.
function gotLoginOKPacket()
    -- Change the state to the main game.
    CEGUI.WindowManager:getSingleton():destroyWindow(getWindow("root"));
    odysic.setState(STATE_GAME);
    odysic.fadeInMusic("music.ogg", 2000);
end

-- Got add character packet.
firstAddCharacterPacket = true;
function gotAddCharacterPacket(chr)
    odysic.addCharacter(chr.x, chr.y, chr.z, chr.mobID, chr.id, chr.red, chr.green, chr.blue, chr.alpha);
    if firstAddCharacterPacket then
        playerID = chr.id;
        firstAddCharacterPacket = false;
    end
end

-- Move request
canMove = true;
function moveRequest(direction)
    if canMove then
        odysic.sendMoveRequestPacket(direction);
        canMove = false;
    end
end

-- Got move object packet
function gotMoveObjectPacket(id, xOff, yOff, zOff)
    if id == playerID then canMove = true end;
    odysic.moveObject(id, xOff, yOff, zOff);
end

function clientDebugMessageImp(msg)
    print(msg);
end
